package com.yld.car.market;

import java.util.ArrayList;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.yld.car.adapter.CarTypeAdapter;
import cn.yld.car.db.RecentlyViewedDBHelper;

import com.yld.car.domain.Car;
import com.yld.car.domain.SearchDetailCar;

public class RecentViewActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.recent_view);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		TextView textView = ((TextView) findViewById(R.id.title));

		textView.setText("最近浏览");

		showBackButton();

		list1 = (ListView) findViewById(R.id.list1);
		list1.setOnItemClickListener(this);
		new Thread(new Runnable() {

			@Override
			public void run() {
				readDataFromDB();

			}
		}).start();
	}

	private ListView list1;

	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position,
			long id) {

		SearchDetailCar s = (SearchDetailCar) adapterView.getAdapter().getItem(
				position);

		System.out.println("test===" + s.getId());
		Intent intent = new Intent();
		intent.setClass(this, CarDetailInfoActivity.class);

		intent.putExtra("cId", s.getId());
		startActivity(intent);
		finish();

	}

	Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			Object o = msg.obj;

			if (o != null) {
				ArrayList<Car> all = (ArrayList<Car>) o;

				// RecentViewAdapter rvAdapter = new RecentViewAdapter(all);
				CarTypeAdapter adapter = new CarTypeAdapter(all,
						getApplicationContext());
				list1.setAdapter(adapter);
			}
		}

	};

	private void readDataFromDB() {

		SQLiteDatabase sDB = mApp.getmDbHelper().getWritableDB();

		Cursor c = sDB.query(RecentlyViewedDBHelper.RECENT_VIEW_TABLE, null,
				null, null, null, null, null);

		ArrayList<Car> all = new ArrayList<Car>();
		while (c.moveToNext()) {

			SearchDetailCar s = new SearchDetailCar();
			s.setId(c.getString(c.getColumnIndex(RecentlyViewedDBHelper.ID)));
			s.setNo(c.getString(c.getColumnIndex(RecentlyViewedDBHelper.NO)));
			s.setName(c.getString(c.getColumnIndex(RecentlyViewedDBHelper.NAME)));
			s.setAdddate(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.ADD_DATE)));
			s.setClassid(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.CLASS_ID)));
			s.setCompanyname(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.COMPANY_NAME)));
			s.setGangkouname(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.GANG_KOU_NAME)));
			s.setGhtime(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.QHTIME)));
			s.setPinpainame(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.PIN_PAI_NAME)));
			s.setPress(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.PRESS)));
			s.setVin(c.getString(c.getColumnIndex(RecentlyViewedDBHelper.VIN)));
			s.setXhorgh(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.XHORQH)));
			s.setXiliename(c.getString(c
					.getColumnIndex(RecentlyViewedDBHelper.XI_LIE_NAME)));
			s.setXppz(c.getString(c.getColumnIndex(RecentlyViewedDBHelper.XPPZ)));
			all.add(s);
		}

		Message msg = mHandler.obtainMessage();
		msg.obj = all;

		mHandler.sendMessage(msg);
	}

	// private class RecentViewAdapter extends BaseAdapter {
	//
	// private ArrayList<SearchDetailCar> all;
	//
	// public RecentViewAdapter(ArrayList<SearchDetailCar> all) {
	// this.all = all;
	// }
	//
	// @Override
	// public int getCount() {
	// // TODO Auto-generated method stub
	// return all.size();
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// // TODO Auto-generated method stub
	// return all.get(position);
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// // TODO Auto-generated method stub
	// return position;
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	//
	// if (convertView == null) {
	//
	// convertView = LayoutInflater.from(getApplicationContext())
	// .inflate(R.layout.car_items, null);
	// }
	//
	// SearchDetailCar s = all.get(position);
	//
	// TextView typeText = (TextView) convertView
	// .findViewById(R.id.itemType);
	// typeText.setText(s.getXhorgh());
	// TextView titleText = (TextView) convertView
	// .findViewById(R.id.itemTitle);
	//
	// titleText.setText(s.getPinpainame());
	// TextView abbreviationText = (TextView) convertView
	// .findViewById(R.id.itemAbbreviation);
	//
	// abbreviationText.setText(s.getName());
	// TextView priceText = (TextView) convertView
	// .findViewById(R.id.itemPrice);
	//
	// priceText.setText(s.getPress() + "万");
	// TextView configText = (TextView) convertView
	// .findViewById(R.id.itemSelectConfig);
	//
	// configText.setText(s.getXppz());
	// TextView publishText = (TextView) convertView
	// .findViewById(R.id.itemPublishDate);
	//
	// publishText.setText(s.getAdddate());
	// TextView dealerText = (TextView) convertView
	// .findViewById(R.id.itemDealerName);
	// dealerText.setText(s.getCompanyname());
	//
	// convertView
	// .setBackgroundResource(android.R.drawable.list_selector_background);
	// return convertView;
	// }
	//
	// }
}
